import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:local_coffee/components/c_row/custom_row.dart';
import 'package:local_coffee/components/c_swiper/custom_swiper.dart';
import 'package:local_coffee/components/hex.dart';
import 'package:local_coffee/components/icon.dart';
import 'package:local_coffee/components/rgba.dart';
import 'package:local_coffee/components/take_out_btn/take_out_btn.dart';
import 'package:local_coffee/provider/counter_model.dart';
import 'package:local_coffee/routes/index.dart';
import 'package:local_coffee/utils/global.dart';
import 'package:provider/provider.dart';

class Home extends StatefulWidget {
  
  static _HomeState _homeState;

  Home() {
    _homeState = _HomeState();
  }

  getAppBar() => _homeState.createAppBar();

  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  
  AppBar createAppBar() {
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          // 头部
          Stack(
            children: <Widget>[
              // 轮播图
              Positioned(
                child: CustomSwiper(
                  [
                    P.swiper1,
                    P.swiper2
                  ],
                  height: G.srceenHeight() > 800 ? 288 : 208,
                ),
              ),
              // 扫描二维码
              Positioned(
                top: 50,
                right: 10,
                child: rightChild(
                  icon: icon0xe60b(
                    size: 20,
                    color: HEX.hexfff
                  ),
                  bgColor: rgba(0, 0, 0, 3),
                  showBorder: false
                ),
              )
            ],
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              children: <Widget>[
                CustomRow(
                  height: 70,
                  padding: EdgeInsets.all(10),
                  leftChild: leftChild(
                    title: 'location',
                    desc: '距离 3 km',
                    titleBold: false
                  ),
                  rightChild: TakeOutBtn(),
                ),
                CustomRow(
                  height: 70,
                  padding: EdgeInsets.all(0),
                  leftChild: leftChild(
                    title: '现在下单',
                    desc: 'order now'
                  ),
                  rightChild: rightChild(
                    icon: icon0xe60a(
                      color: RGBA.rgba63473a,
                      size: 20
                    )
                  ),
                  onPressed: () => G.pushNamed(
                    N.menu,
                    arguments: {'msg': 'home to menu'}
                  ),
                ),

                /// 测试 provider 局部刷新
                Consumer<CounterModel>(
                  builder: (BuildContext context, CounterModel _counter, Widget child) {
                    return CustomRow(
                      height: 70,
                      padding: EdgeInsets.all(0),
                      leftChild: leftChild(
                        title: '咖啡钱包${_counter.value}',
                        desc: 'coffee wa!!et'
                      ),
                      rightChild: rightChild(
                        icon: icon0xe607(
                          color: RGBA.rgba68443c,
                          size: 20
                        )
                      ),
                      onPressed: () {
                        _counter.add();
                      },
                    );
                  },
                ),
                CustomRow(
                  height: 70,
                  padding: EdgeInsets.all(0),
                  leftChild: leftChild(
                    title: '送 Ta 哎呀',
                    desc: 'send Ta Coffee'
                  ),
                  rightChild: rightChild(
                    icon: icon0xe608(
                      color: RGBA.rgba68443c,
                      size: 20
                    )
                  ),
                  onPressed: () {
                    G.toast('加购成功');
                  },
                ),
                CustomRow(
                  height: 70,
                  padding: EdgeInsets.all(0),
                  leftChild: leftChild(
                    title: '组件LIST',
                    desc: 'components List'
                  ),
                  rightChild: rightChild(
                    icon: icon0xe600(
                      color: RGBA.rgba68443c,
                      size: 20
                    )
                  ),
                  onPressed: () => G.pushNamed(
                    N.exampleWidgetsList,
                    arguments: {'msg': 'home to example widgets list'}
                  ),
                ),
                Container(
                  child: Image.asset(P.bottomBar),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Container rightChild({
    Widget icon,
    Color bgColor,
    bool showBorder = false
  }) {
    return Container(
      width: 40,
      height: 40,
      decoration: BoxDecoration(
        color: bgColor,
        border: Border.all(
          color: showBorder ? RGBA.rgba63473a : Colors.transparent
        ),
        borderRadius: BorderRadius.circular(20)
      ),
      child: Container(
        padding: EdgeInsets.only(left: 2),
        child: icon,
      ),
    );
  }

  Column leftChild({
    String title,
    String desc,
    bool titleBold = true
  }) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Row(
          children: <Widget>[
            Text(
              title,
              style: TextStyle(
                color: RGBA.rgba383838,
                fontSize: 16,
                fontWeight: titleBold ? FontWeight.bold : FontWeight.normal
              ),
            )
          ],
        ),
        Row(
          children: <Widget>[
            Text(
              desc,
              style: TextStyle(
                color: RGBA.rgba808080,
                fontSize: 12
              ),
            )
          ],
        )
      ],
    );
  }
}